import { mergeOptions } from "./utils";

export function initGlobalAPI(Vue){
    Vue.options = {
        _base : Vue
    };
    Vue.mixin = function(mixin){
        this.options = mergeOptions(this.options,mixin);

        return this;
    }

    Vue.extend = function(options){
        function Sub(options = {}){
            this._init(options);//this 为sub的实例 new Sub
        }
        Sub.prototype = Object.create(Vue.prototype);//Sub.prototype.__proto__ ===Vue.prototype
        Sub.prototype.constructor = Sub;

        //希望将用户传递的参数 和全局的Vue.options来合并
        Sub.options = mergeOptions(Vue.options,options);//保存用户传递的选项
        return Sub
    }
    Vue.options.components = {};//全局的指令 Vue.options.directives
    Vue.component = function(id,definition){
        //如果definition已经是一个函数了 说明用户自己调用了Vue.extend
        definition = typeof definition === 'function' ? definition : Vue.extend(definition);
        Vue.options.components[id]=definition;
        console.log(Vue.options.components);
    }
}